package nju.bishe.weibo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;


import nju.bishe.weibo.service.LaughChannelService;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @author longwei.wu
 * @version $Id: LaughSendActivity.java, v 0.1 2011-4-5 下午01:52:20 longwei.wu Exp $
 */
public class LaughSendActivity extends Activity {

    private TextView editText;
    private Button send;
    private Button imageChooseButton;
    private Button camera;
    private String file;
    private ImageView imageView = null;
    private static final int IMAGE_MAX_SIZE = 100;
    private ProgressDialog progressDialog;
    

    private Handler handler = new Handler(){
    	@Override
		public void handleMessage(final Message msg) {
    		progressDialog.dismiss();
    	}
    };
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.laugh_send);
        editText = (TextView) findViewById(R.id.laugh_send_text);
        send = (Button) findViewById(R.id.laugh_text_send);
        camera = (Button) findViewById(R.id.laugh_text_camera);
        imageChooseButton = (Button) findViewById(R.id.laugh_choose_images_button);
        String imageFile = getIntent().getStringExtra("imagePath");
        file = imageFile;
        if(imageFile!=null){
            
            imageView = (ImageView) findViewById(R.id.laugh_image_camera);
            Bitmap bitmap = decodeFile(new File(imageFile));
            imageView.setImageBitmap(bitmap);
              
        }
        send.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                String content = editText.getText().toString();
                send(content);
                new AlertDialog.Builder(LaughSendActivity.this)
                .setIcon(R.drawable.icon)
                .setTitle("发布成功").setPositiveButton("确定", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int whichButton) {
                       dialog.dismiss(); 
                    }
                }).show();
                
                editText.setText("");
            }
        });
        
        camera.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
           
                Intent intent = new Intent(LaughSendActivity.this,SimpleCamera.class);
                startActivity(intent);
            }
        });
    }
    
    private Bitmap decodeFile(File f){
        Bitmap b = null;
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);
            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            b = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
        }
        return b;
    }
    
    private void send(String content){
		this.progressDialog = ProgressDialog.show(this, " Working...",
				" Sending Message", true, false);
		LaughChannelService channelService = new LaughChannelService();
		if(file!=null&&!file.equals(""))
			channelService.updateStatus(content,file);
		else
			channelService.updateStatus(content);
		new Thread() {
			@Override
			public void run() {

			//invoke the method to send
				handler.sendEmptyMessage(0);
			}
		}.start();
    }
}

